<?php
	require_once ('checks.php'); 
	require_once ('Database.php'); 
	$db= new DB();
	$arr = $db->findName("news");
	$n= count($arr);
	if (isset($_GET['name'])){
		$by= "name";
		if (!strcmp($_GET['name'], 'asc')){
			$way = "asc";	
		} else {
			$way = "dsc";
		}
				
	} else {
		$by = "order";
		if (!strcmp($_GET['order'], 'asc')){
			$way = "asc";	
		} else {
			$way = "dsc";
		}
	}
	
	$newsarr= sortBy($arr, $by, $way);
	if (isset($_POST['go'])){
		$newsarr = reOrder($_POST['order_value'],$newsarr);
		reOrderNewsInDatabase($newsarr);
		$newsarr= sortBy($arr, $by, $way);
	}
	

	
?>	
		<form method="POST" action="?page=choose_news<?php
														if (isset($_GET['name'])) {
															echo "&name=";
															if(!strcmp($_GET['name'],'asc')){
																	echo 'asc';
																} else {
																	echo 'dsc';
																}
														}
														if (isset($_GET['order'])){
															echo "&order=";
															if (!strcmp($_GET['order'],'asc')){
																			echo 'asc';
																		} else {
																			echo 'dsc';
																		}
														}
		
		
														?>" id="choose_news">
			<table border='1' width="400">
				<tr>
					<th> 
						<a href='?page=choose_news&name=<?php if (isset($_GET['name']) && !strcmp($_GET['name'],'asc')){
																	echo 'dsc';
																} else {
																	echo 'asc';
																}
															?>'> 
							Name
						</a>
					</th>
					<th> 
						Edit
					</th>
					<th colspan="2"> <a href='?page=choose_news&order=<?php if (isset($_GET['order']) && !strcmp($_GET['order'],'asc')){
																			echo 'dsc';
																		} else {
																			echo 'asc';
																		}
																		?>'>
								Order 
						</a>
					</th>
															
				</tr>
							<?php
								for ($i=0;$i<$n;$i++) {
									
									echo "<tr>";
									echo "	<td><center>".$newsarr[$i]->getName()."</center></td>";
									echo "	<td width='35'><a href='?page=edit_news&news_name=".$newsarr[$i]->getName()."'><img src='images/edit.jpg' width='35'></a></td>";
									echo "	<td width='35'><input type='text' name='order_value[$i]' size='1' value='".intval($newsarr[$i]->getOrder())."'></td>";
									echo "	<td width='35'><input type='submit' name='go' class='submit_select' value=''></td>";
									echo"</tr>";
								}
							?>

			</table>
		</form>
